﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IMS_Data;

namespace IMS_WEB
{
    public partial class NewProdect : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }



        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            IMS_Data.ProductReposity pr = new IMS_Data.ProductReposity();

            product pro = new product();
            pro.product_name = tbName.Value;
            pro.product_type = tbType.Value;
            pro.product_code = tbProductCode.Text;
            pro.product_sn = tbProductSN.Text;
            pro.product_mainClass = tbProductMainClass.Text;
            pro.product_price = string.IsNullOrEmpty(tbPrice.Value) ? -1 : Convert.ToDecimal(tbPrice.Value);
            pro.remarks = tbRemark.Value;

            pro.product_unit = tbUnit.Value;
            //pro.product_salePrice = string.IsNullOrEmpty(tbSalePrice.Value) ? -1 : Convert.ToDecimal(tbSalePrice.Value);

            IMS_Data.CompanyReposity cr = new IMS_Data.CompanyReposity();

            company cp = cr.GetCompany(tbCompany.Text);
            if(cp == null)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加失败，生产厂家不存在!')</script>");
            }
            else
            {
                pro.company_id = cp.index_id;

                if (pr.ExistProduct(pro))
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('该商品外部编号已经存在，无法添加!')</script>");
                }
                else
                {
                    pr.AddProduct(pro);

                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加成功!')</script>");
                }
               
            }
            
            
        }
    }
}